import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {BasicConfirmModalComponent} from "../../base-modal";
import {NzModalRef, NzModalService} from "ng-zorro-antd/modal";
import {Observable, of} from "rxjs";
import {BasicInformationList} from "../../../core/services/http/classification-identification/basic-information.service";


@Component({
  selector: 'app-msds-modals',
  templateUrl: './msds-modals.component.html',
  styleUrls: ['./msds-modals.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MsdsModalsComponent extends BasicConfirmModalComponent implements OnInit {
  dataList: BasicInformationList[] = [];
  params: object;

  constructor(private modalRef: NzModalRef, private cdr: ChangeDetectorRef, private modalSrv: NzModalService,) {
    super();
    this.params = {}
  }

  protected getCurrentValue(): Observable<any> {
    if (this.dataList.length > 0) {
      const tempArrays: number[] = [];
      this.dataList.forEach((item) => {
        tempArrays.push(item.id);
      });
      return of(tempArrays)
    } else {
      return of(false);
    }
  }

  Delete(id: number): void {
    this.modalSrv.confirm({
      nzTitle: '确定要移除吗？',
      nzContent: '',
      nzOkText: '确定',
      nzCancelText: '取消',
      nzOnOk: () => {
        this.dataList.splice(this.dataList.findIndex(item => item.id === id), 1);
        this.dataList = [...this.dataList]
        this.cdr.detectChanges();
      },
      nzOnCancel: () => {
        return
      }
    });
  }


  ngOnInit(): void {
    // @ts-ignore
    this.dataList = this.params.arrays;
  }

}
